home *** CD-ROM | disk | FTP | other *** search
- Path: bmtlh10.bnr.ca!news
- From: John Hickin <hickin@bnr.ca>
- Newsgroups: comp.lang.c++
- Subject: Re: () syntax for constructor for an array?
- Date: 7 Mar 1996 14:20:55 GMT
- Organization: Bell Northern Research
- Message-ID: <4hmrc7$1dc@bmtlh10.bnr.ca>
- References: <NICKC.96Mar7121149@uxe.liv.ac.uk>
- NNTP-Posting-Host: bmtlh520.bnr.ca
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 1.1 (X11; I; HP-UX A.09.05 9000/715)
- X-URL: news:NICKC.96Mar7121149@uxe.liv.ac.uk
-
- There doesn't seem a useful way in C++ to initialize an array of objects
- unless the initial values are all the same. When this restriction does
- hold, however, you have to ensure that the default constructor passes in
- the right value.
-
- These days you might be able to use a template to ease the pain of setting
- up more general default values:
-
- struct B { B(int = 42); B(const char*, float); };
-
- template < int InitialValue >
- struct B1 : public B { B1() : B(InitialValue) {} };
-
- template < const char* Id , float F >
- struct B2 : public B { B2() : B(Id,F) {} };
-
- I can't be sure because the above template feature isn't supported by any
- compiler I have access to. A code generator I had the pleasure of working
- on, however, employed this "derive a class for initialization" trick. I
- found that it tended to generate a lot of code.
-
- --
- John Hickin Nortel Technology, Montreal, Quebec
- (514) 765-7924 hickin@bnr.ca
-
-